<?php
/**
 * TeamPlayersController.class.php
 *
 * PHP version 5
 *
 * Copyright (c) 2010, Leo Valen  http://www.leovalen.no
 * All rights reserved.
 *
 * @author		Leo Valen <leo@code.coop>
 */
/**
 * Principal controller class for handling requests
 *
 * @version		1.0
 */
namespace TeamPlayers;
class Controller
{
	/**
	 * Smarty template
	 *
	 * @var string
	 * @access protected
	 */
	protected $template;
	/**
	 * Constructor
	 *
	 * @return		void
	 * @access		public
	 */
	public function __construct()
	{
	    $this->template = "";
	    TeamPlayers::$smarty->assign( 'controller', $this );
	    TeamPlayers::$smarty->assign( 'stack', TeamPlayers::$stack );
	    TeamPlayers::$smarty->assign( 'db', TeamPlayers::$db );
	    $this->executeActionMethod();
	    $this->executeViewMethod();
	}
	/**
	 * First checks whether a view is specified via GET. Else, it is determined from the URL, or defaulted to the 404 Not Found view.
	 *
	 * @return		void
	 * @access		public
	 */
	public function executeViewMethod()
	{
	    if ( isset( $_GET['view'] ) && is_string( $_GET['view'] ) )
	    {
	        $method = $_GET['view'] . 'View';
	        if ( method_exists( $this, $method ) )
	        {
	            $this->$method();
	            return;
	        }
	    }
	    if ( empty( TeamPlayers::$stack ) )
	    {
	        $this->defaultView();
	        return;
	    }
	    $stack = TeamPlayers::$stack;
	    $method = array_shift( $stack );
	    foreach ( $stack as $element )
	    {
	        $method .= ucfirst( $element );
	    }
	    $method .= "View";
	    if ( method_exists( $this, $method ) )
	    {
	        $this->$method();
	    }
	    else
	    {
	        $this->notFoundView();
	    }
	}
	/**
	 * Execute action method if specified
	 *
	 * @return		void
	 * @access		public
	 */
	public function executeActionMethod()
	{
	    if ( isset( $_GET['action'] ) )
	    {
	        $method = $_GET['action'] . 'Action';
	        if ( method_exists( $this, $method ) )
	        {
	            $this->$method();
	        }
	    }
	}
	/**
	 * Displays the default view
	 *
	 * @return		void
	 * @access		public
	 */
	public function defaultView()
	{
	    global $start_time;
	    TeamPlayers::$smarty->assign( 'executionTime', round( ( microtime( true ) - $start_time ) * 1000 ) );
	    if ( ! empty( $this->template ) )
	    {
	        TeamPlayers::$smarty->display( $this->template );
	    }
	}
	/**
	 * Displays a 404 Not Found page
	 *
	 * @return		void
	 * @access		public
	 */
	public function notFoundView()
	{
	    header("HTTP/1.0 404 Not Found"); 
	    $this->setSmartyTemplate( "TeamPlayers/404.tpl" );
	    TeamPlayers::$smarty->display( $this->template );
	}
	/**
	 * Example action
	 *
	 * @param       int       $number
	 * @param       string    $string
	 * @return		void
	 * @access		public
	 */
	public function exampleAction( $number = '', $string = '' )
	{
	    /*
	     * Define rules for validating the input
	     */
	    $filter_rules = array(
	            'number' => FILTER_VALIDATE_INT,
	            'string' => FILTER_SANITIZE_STRING
	    );
	    $input = $this->getActionInput( func_get_args(), $filter_rules, INPUT_GET );
	    var_dump( $input );
	}
	/**
	 * Determines whether to use regular function arguments or external input for an action and filters the input data
	 *
	 * @param       array    Arguments from func_get_args() in the action method calling this method
	 * @param       mixed    Filter rules for validation and sanitation
	 * @return		array
	 * @access		protected
	 */
	protected function getActionInput( $arguments, $filter_rules, $external_input_source )
	{
	    /*
	     * Use input from GET or POST if the argument vector is empty
	     */
	    if ( empty( $arguments ) )
	    {
	        if ( function_exists( 'filter_input_array' ) )
	        {
	            $data = filter_input_array( $external_input_source, $filter_rules );
	        }
	        else
	        {
	            /*
	             * Temporary workaround; skip filtering if the filter module is unavailable
	             */
	            foreach ( $filter_rules as $key => $value )
	            {
	                if ( $external_input_source == INPUT_GET )
	                {
	                    $data[$key] = $_GET[$key];
	                }
	                else if ( $external_input_source == INPUT_POST )
	                {
	                    if ( isset( $_POST[$key] ) )
	                    {
	                        $data[$key] = $_POST[$key];
	                    }
	                    else
	                    {
	                        $data[$key] = null;
	                    }
	                }
	            }
	        }
	    }
	    /*
	     * Use the supplied arguments if the argument vector is not empty
	     */
	    else
	    {
	        $external_input = array();
	        $i = 0;
	        foreach ( $filter_rules as $argument => $value )
	        {
	            if ( isset( $arguments[$i] ) )
	            {
	                $external_input[$argument] = $arguments[$i];
	            }
	            ++$i;
	        }
	        $data = filter_var_array( $arguments, $filter_rules );
	    }
	    return $data;
	}
	/**
	 * Terminates the current session
	 *
	 * @return		void
	 * @access		public
	 */
	public function logoutAction()
	{
	    TeamPlayers::$ac->logout();
	}
	/**
	 * Displays a 404 Not Found page
	 *
	 * @param       string  $template   Template filename relative to template directory
	 * @return		bool                True if the specified template exists and was successfully set
	 * @access		public
	 */
	public function setSmartyTemplate( $template )
	{
	    if ( @TeamPlayers::$smarty->template_exists( $template ) )
	    {
	        $this->template = $template;
	    }
	}
}
?>